package com.cpi.cornpp.plantinitialcost.service;

import java.util.Iterator;
import java.util.List;

import com.cpi.cornpp.plantinitialcost.converter.PlantInitialCostConverter;
import com.cpi.cornpp.plantinitialcost.dao.PlantInitialCostDAO;
import com.cpi.cornpp.plantinitialcost.dto.PlantInitialCostDTO;
import com.cpi.cornpp.plantinitialcost.exception.PlantInitialCostException;
import com.cpi.cornpp.plantinitialcost.view.PlantInitialCostGroupView;
import com.cpi.cornpp.plantinitialcost.view.PlantInitialCostView;

public class PlantInitialCostService {
	public List getPlantInicialCost(int versionId)throws PlantInitialCostException{
		List plantInitialCostGroupViews = null;
		List plantInitialCostDTOs = null;
		PlantInitialCostDAO plantInitialCostDAO = null;
		PlantInitialCostConverter plantInitialCostConverter = null;
		try{
			plantInitialCostDAO = new PlantInitialCostDAO();
			plantInitialCostConverter = new PlantInitialCostConverter();
			plantInitialCostDTOs = plantInitialCostDAO.initialCostPerVersion(versionId);
			plantInitialCostGroupViews = plantInitialCostConverter.convertPlantInitialCostDTOsToViews(plantInitialCostDTOs);
		}catch(PlantInitialCostException plantInitialCostException){
			throw plantInitialCostException;
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_SERVICE, PlantInitialCostException.ACTION_LISTS);
			throw plantInitialCostException;
		}
		return plantInitialCostGroupViews;
	}
	
	public void updatePlantInitialCost(List plantInitialCostGroupViews)throws PlantInitialCostException{
		PlantInitialCostDAO plantInitialCostDAO = null;
		PlantInitialCostConverter plantInitialCostConverter = null;
		PlantInitialCostGroupView plantInitialCostGroupView = null;
		PlantInitialCostView plantInitialCostView = null;
		PlantInitialCostDTO plantInitialCostDTO = null;
		Iterator groupIterator = null;
		Iterator detailIterator = null;
		int groupSize = 0;
		int detailSize = 0;
		try{
			plantInitialCostDAO = new PlantInitialCostDAO();
			plantInitialCostConverter = new PlantInitialCostConverter();
			groupIterator = plantInitialCostGroupViews.iterator();
			groupSize = plantInitialCostGroupViews.size();
			for( int index = 0; index < groupSize; index++ ){
				plantInitialCostGroupView = (PlantInitialCostGroupView)groupIterator.next();
				detailSize = plantInitialCostGroupView.getPlantInitialCostViews().size();
				detailIterator = plantInitialCostGroupView.getPlantInitialCostViews().iterator();
				for( int detailIndex = 0; detailIndex < detailSize; detailIndex++ ){
					plantInitialCostView = (PlantInitialCostView)detailIterator.next();
					plantInitialCostDTO = plantInitialCostConverter.convertPlantInitialCostViewToDTO(plantInitialCostView);
					plantInitialCostDAO.updateInitialCost(plantInitialCostDTO);
				}
			}
		}catch(PlantInitialCostException plantInitialCostException){
			throw plantInitialCostException;
		}catch(Exception exception){
			PlantInitialCostException plantInitialCostException = null;
			exception.printStackTrace(System.out);
			plantInitialCostException = new PlantInitialCostException(exception, PlantInitialCostException.LAYER_SERVICE, PlantInitialCostException.ACTION_UPDATE);
			throw plantInitialCostException;
		}
	}
}